package cn.bingo.yunsj.service;

import cn.bingo.yunsj.common.StateCode;
import cn.bingo.yunsj.common.exception.EmException;
import cn.bingo.yunsj.common.exception.OwnException;
import cn.bingo.yunsj.domain.RpModel;
import cn.bingo.yunsj.mail.model.FileProperties;
import cn.bingo.yunsj.mapper.RpModelMapper;
import cn.bingo.yunsj.common.result.Response;
import cn.bingo.yunsj.utils.WordToPDFConvertor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ModelService {

    @Autowired
    private RpModelMapper modelMapper;

    /**
     * 获取数据库所有模板信息
     */
    public Response getAll(){
        List<RpModel> rpModels = modelMapper.selectAll();
        return new Response(StateCode.FIND_SUCCESS, (long) rpModels.size(),rpModels);
    }

    /**
     * to do这里还需要限定启动个数，针对类型限制
     * @param rpModel
     * @return
     */
    public Response state(RpModel rpModel) {
        if(StringUtils.equals("已启动",rpModel.getModelState())){
            throw new OwnException(EmException.STATE_FAIL);
        }

        /**
         * 限制启动类型，对于新入职员工贺卡，最后只能启动三个
         */
        if(StringUtils.equals("新入职员工贺卡",rpModel.getModelType())){
            List<RpModel> list = modelMapper.selectByRuleAndState(rpModel.getModelType(),"已启用");
            if(list.size()>=3){
                throw new OwnException(EmException.NEW_FAIL);
            }
        }else{
            List<RpModel> list = modelMapper.selectByRuleAndState(rpModel.getModelType(),"已启用");
            if(list.size()>=1){
                throw new OwnException(EmException.PARAM_EXCPTION);
            }
        }

        modelMapper.updateState("已启用",rpModel.getModelId());
        return new Response(StateCode.STATE);
    }

    public Response delete(RpModel rpModel) {
        modelMapper.deleteByPrimaryKey(rpModel.getModelId());
        return new Response(StateCode.DELETE_SUCCESS);
    }

    public Response search(RpModel rpModel) {
        String type = "%"+rpModel.getModelType()+"%";
        List<RpModel> rpModels = modelMapper.search(type);
        return new Response(StateCode.SEARCH_SUCCESS, (long) rpModels.size(),rpModels);
    }

    public Response getNoRule() {
        List<RpModel> rpModels = modelMapper.searchNoRule();
        return new Response(StateCode.SEARCH_SUCCESS, (long) rpModels.size(),rpModels);
    }

    public Response disable(RpModel rpModel) {
        if(StringUtils.equals("未启用",rpModel.getModelState())){
            throw new OwnException(EmException.STATE_FAIL);
        }
        modelMapper.updateState("未启用",rpModel.getModelId());
        return new Response(StateCode.DISABLE);
    }

    public Response upload(String type,MultipartFile file) {
        // 校验一下，对于已存在得模板，更新
        File file1 = new File(FileProperties.getDocPath() + file.getOriginalFilename());
        try {
            file.transferTo(file1);
            String wordPath = FileProperties.getDocPath()+file.getOriginalFilename();
            String pafPath = FileProperties.getPdfPath()+file.getOriginalFilename();
            WordToPDFConvertor.wordToPDFConvertor(wordPath,pafPath);
        } catch (IOException e) {
            throw new OwnException(EmException.FILE_EXCPTION, e.getMessage());
        }

        RpModel rpModel = new RpModel();
        rpModel.setModelId(UUID.randomUUID().toString());
        rpModel.setUpdatedBy("YUNSJ");
        rpModel.setCreatedBy("YUNSJ");
        rpModel.setUpdatedTime(new Date());
        rpModel.setCreatedTime(new Date());
        rpModel.setModelName(file.getOriginalFilename());
        rpModel.setModelType(type);
        rpModel.setModelState("未启动");
        rpModel.setModelPath(FileProperties.getDocPath()+file.getOriginalFilename());
        modelMapper.insert(rpModel);
        return new Response(StateCode.UPLOAD_SUCCESS);
    }
}
